/*
 * Project: D-Link TR-069
 * 
 * File:   data.h
 * 
 * Authors: Levon Baghramyan		
 *
 * Created on September 20, 2011, 11:53 AM
 * 
 * 
 * This object is useful to creating log for ACS_Server e.g.
 * create log and trace files
 * 
 * Copyright (C) 2011 D-Link Company.  All rights reserved.
 */

#include "include/acsLogger.h"

// variables for the parameters of the configuration file
char s_mail[1000], trace_m[1000], log_m[100], Email[1000], f_name[100], rtime[100];

// Time of minutes, hours, day and month
char RDATA_m[10], RDATA_H[10], RDATA_D[10], RDATA_M[10];

FILE *f,*f1,*f2;

// path for log and trace
char conf_path[100];

// path of the current directory
char conf_path1[100];
int trace_OnOff;

//flags
int conf_level=5, s_level=5;

/*
 * parser settings from the configuration file
 *
 * the input parameter string to the desired parameter
 * output parameter of an option with no additional characters
 */
int conf_parser(char *pars_s)
{
	int c=0,lo,i,j,k=0;
	char *path;
	char s[1000];
	memset(s,'\0',1000);
	lo=(ftell(f2)-strlen(pars_s));
	// move file pointer to beginning of line
	fseek(f2, lo, SEEK_SET);

	// reads the whole line
	path=fgets(s,1000,f2);
	path=s;

	/*
	 * removes "=" with the line
     */

	for(i=0; i<strlen(path); i++)
	{
		if(path[i]=='=')
		{
			for(j=i+1;j<strlen(path)+1;j++)
			{
				k=c++;
				s[k]=path[j];
			}
		}
	}


	/*
	 * removes '"' and blank characters with the line
	 */

	for(i=0; i<=strlen(s);i++)
	{
		if((s[i]=='"') || (s[i]==' ') )
		{
			for (j=i; j<=strlen(s); j++)
			{
				s[j]=s[j+1];
			}

			--i;
		}

	}

    fseek(f2, lo+1, SEEK_SET);
    strcpy(pars_s,s);

	return 0;
}

/*
 * variables the configuration file
 *
 * output variables from the configuration file
 */

int params_reading(char *name, int *r, int *level, int *trace, char *mail, char *path)
{
	char copy_name[200],conf_name[10],parser_name[200];
	int i,flag=0,conf_l=100;
        //*r=0;
	memset(copy_name,'\0',200);
	memset(parser_name,'\0',200);
	strcpy(copy_name,name);
	strcpy(parser_name,name);
	memset(conf_name,'\0',10);
	/*
	 * converts all text to uppercase
	 */
	for(i=0; i < strlen(copy_name); i++)
	    copy_name[i] = toupper(copy_name[i]);

	/*
	 * looking for the right parameter names
	 */
	switch(copy_name[0])
	{
	case 'L':
	{
		strcpy(conf_name,"LEVEL");
		conf_l=0;
		break;
	}
	case 'T':
	{
		strcpy(conf_name,"TRACE");
		conf_l=1;
		break;
	}
	case 'M':
	{
		strcpy(conf_name,"MAILTO");
		conf_l=2;
		break;
	}
	case 'P':
	{
		strcpy(conf_name,"PATH");
		conf_l=3;
		break;
	}
	case 'R':
	{
		strcpy(conf_name,"REPEAT");
		conf_l=4;
		break;
	}
	default:break;
	}

	/*
	 * checks whether the coincidence with the desired name
	 */
	for(i=0;i<=strlen(conf_name);i++)
	{
		if (copy_name[0]==conf_name[0])
		{
			++flag;
		}
	}

	/*
	 * parsing to the parameters
	 */
	if (strlen(conf_name)==(flag-1))
	{
		conf_parser(parser_name);
		switch(conf_l)
		{
		case 0:
		{
			*level=atoi(parser_name);
			break;
		}
		case 1:
		{
			*trace=atoi(parser_name);
			break;
		}
		case 2:
		{
			strcpy(mail,parser_name);
			break;
		}
		case 3:
		{
			strcpy(path,parser_name);
			break;
		}
		case 4:
		{
			if((strlen(parser_name)-1)==strlen("DD.MM.YY:HH:MM")) *r=0;
			if((strlen(parser_name)-1)==strlen("DD.MM.YY:HH")) *r=1;
			if((strlen(parser_name)-1)==strlen("DD.MM.YY")) *r=2;
			if((strlen(parser_name)-1)==strlen("MM.YY")) *r=3;
			break;
		}
		default: break;
		}
	}

	return 0;
}
/*
 * reads the configuration file
 *
 * output parameters
 * path for LOG and TRACE files, Email address, error level, trace ON or OFF, recurrence creation LOG and TRACE files
 */

int read_conf(char *s_path, char *s_mail,int *s_level, int *s_trace, char *repeat)
{
	/*
	 * first is read the config file?
	 */

	if (*s_level==5)
	{
		FILE *f3;
		int rtt;
		char *path=NULL;
		memset(conf_path,'\0',100);
		memset(conf_path1,'\0',100);
		int size;
		size=100;
		path=getcwd(path,size);
		strcpy(conf_path1,path);
		strcat(conf_path1,"/");
		strcpy(conf_path,path);
                
		strcat(conf_path,"/config/log-trace.config");
		f2=fopen(conf_path,"r");
		char s1[1000],r_path[100],s11[100];
		strcpy(r_path,conf_path1);
		strcat(r_path,"ACS-ERROR.LOG\0");
		while (fscanf(f2, "%s", s1) != EOF)
		{

			/*
			 * ignore commented lines
			 */

			if(((s1[0]=='/') && (s1[1]=='/')) || (s1==NULL) || (s1[0]=='#') || ((s1[0]=='/') && (s1[1]=='*')))
			{continue;}
			else
			{
				params_reading(s1,&rtt, s_level, s_trace, s_mail, s_path);
                              
			}

		}

		/*
		 * checks the parameters of the configuration file
		 */

		if((*s_level>4) || (*s_level<0))
		{
			f3=fopen(r_path,"a+");
			char str2[80];
			lt = time(NULL);
			ptr = localtime(&lt);
			strftime(str2, 100, "%H:%M:%S", ptr);
			fprintf(f3,"%s %s %s\n",str2,"incorrect LEVEL.",conferr);
			*s_level=1;
			fclose(f3);
		}
		
		if((*s_trace>2) || (*s_trace<0))
		{
			f3=fopen(r_path,"a+");
			char str2[80];
			lt = time(NULL);
			ptr = localtime(&lt);
			strftime(str2, 100, "%H:%M:%S", ptr);
			fprintf(f3,"%s %s %s\n",str2,"incorrect TRACE.",conferr);
			*s_trace=1;
			fclose(f3);
		}
		
		if((strlen(s_mail)<8))
		{
			f3=fopen(r_path,"a+");
			char str2[80];
			lt = time(NULL);
			ptr = localtime(&lt);
			strftime(str2, 100, "%H:%M:%S", ptr);
			fprintf(f3,"%s %s %s\n",str2,"email list is empty.",conferr);
			memset(s_mail,'\0',100);
			fclose(f3);
		}
		memset(s11,'\0',100);
		strcpy(s11,s_path);
		strcat(s11,"ACS");

		if(!fopen(s11,"w"))
		{
			f3=fopen(r_path,"a+");
			char str2[80];
			lt = time(NULL);
			ptr = localtime(&lt);
			strftime(str2, 100, "%H:%M:%S", ptr);
			memset(s_path,'\0',1000);
			system("mkdir conf_log-trace");
			fprintf(f3,"%s %s %s\n",str2,"Can not open input file.",conferr);
			fclose(f3);

		}


		/*
		 * date format for the name of LOG and TRACE files
		 */
		char str2[80];
		memset(repeat,'\0',10);
		memset(RDATA_m,'\0',10);
	    memset(RDATA_H,'\0',10);
	    memset(RDATA_D,'\0',10);
	    lt = time(NULL);
	    ptr = localtime(&lt);
		switch(rtt)
		{
		case 0:
		{
			strcpy(repeat,DATA_m);
			memset(RDATA_M,'\0',10);
			break;
		}
		case 1:
		{
			strcpy(repeat,DATA_H);
			memset(RDATA_M,'\0',10);
			strftime(str2, 100, "%M", ptr);
			strcpy(RDATA_m,str2);
			break;
		}
		case 2:
		{
			strcpy(repeat,DATA_D);
			memset(RDATA_M,'\0',10);
			strftime(str2, 100, "%M", ptr);
			strcpy(RDATA_m,str2);
			strftime(str2, 100, "%H:", ptr);
			strcpy(RDATA_H,str2);
			break;
		}
		case 3:
		{

			strcpy(repeat,DATA_M);
			strftime(str2, 100, "%M", ptr);
			strcpy(RDATA_m,str2);
			strftime(str2, 100, "%H:", ptr);
			strcpy(RDATA_H,str2);
			strftime(str2, 100, "%d", ptr);
			strcpy(RDATA_D,str2);
			strftime(str2, 100, "%d-%m ", ptr);
			strcpy(RDATA_M,str2);

			break;
		}
		default:break;
		}
		}

	fclose(f2);
	
	return 0;

}

/*
 * send mail
 *
 * input parameter message
 */

int sendmail(char *mailmessage)
{
	/*
	 * first is read the config file?
	 */
	if(conf_level==5)
		{
			read_conf(f_name, s_mail,&conf_level,&trace_OnOff,rtime);
			int k=strlen(f_name);
			char tr[k-1];
			memset(tr,'\0',k-1);
			strcpy(tr,f_name);
			strcpy(trace_m,tr);
			trace_m[strlen(trace_m)-1]='\0';
			f_name[strlen(f_name)-1]='\0';
			if (f_name[strlen(f_name)-1]!='/')
			{
				strcat(f_name,"/");
			}

			if (trace_m[strlen(trace_m)-1]!='/')
			{
				strcat(trace_m,"/");
			}
			strcpy(log_m,f_name);
			k=strlen(s_mail);
			char tm[k-1];
			memset(tm,'\0',k-1);
			strcpy(tm,s_mail);
			strcpy(Email,tm);
			Email[strlen(Email)-1]='\0';
		}
	logtime(f_name,rtime);
	f1=fopen(f_name,"a+");
	   fclose(f1);

	/*
	 * asked to check the configuration file EMAIL address
	 */
	if(Email!=NULL)
	{
		char message[24]={0x0};
		char cmd[128]={0x0};
		if (conf_level==5)
		strcpy(message,mailmessage);

		/*
		 * sends messages to EMAIL
		 */

		sprintf(cmd, "echo \"%s.\" | sendmail %s ",message,Email);
		system(cmd);
	}

	return 0;
}

/*
 *  defines a new name for the LOG and TRACE file
 *
 *  output parameter name LOG and TRACE files
 *  the input parameter setting of the configuration file
 */

int logtime(char *s_name, char *rtime)
{
	/*
	 * create a file name
	 */
	char s[100];
	char str[100];
	memset(s,'\0',100);
	strcpy(s,log_m);
	strcat(s,"\0");
	lt = time(NULL);
	ptr = localtime(&lt);
	strcat(s,"ACS-");
	strcat(s,RDATA_D);
	strftime(str, 100, rtime, ptr);
	strcat(s,str);
	strcat(s,RDATA_H);
	strcat(s,RDATA_m);
	strcat(s,".log");
	memset(s_name,'\0',100);
	strcpy(s_name,s);

	return 0;

}

/*
 * opens the trace file to print message
 *
 * input parameter message
 */

int trace(char *message)
{

	/*
	 * first is read the config file?
	 */
	if(conf_level==5)
        {
		read_conf(f_name, s_mail,&conf_level,&trace_OnOff,rtime);
		int k=strlen(f_name);
		char tr[k-1];
		memset(tr,'\0',k-1);
		strcpy(tr,f_name);
		strcpy(trace_m,tr);
		trace_m[strlen(trace_m)-1]='\0';
		f_name[strlen(f_name)-1]='\0';
		if (f_name[strlen(f_name)-1]!='/')
		{
			strcat(f_name,"/");
		}

		if (trace_m[strlen(trace_m)-1]!='/')
		{
			strcat(trace_m,"/");
		}
		strcpy(log_m,f_name);
		k=strlen(s_mail);
		char tm[k-1];
		memset(tm,'\0',k-1);
		strcpy(tm,s_mail);
		strcpy(Email,tm);
		Email[strlen(Email)-1]='\0';
	}

	/*
	 * create a file name
	 */
	logtime(f_name,rtime);
	f1=fopen(f_name,"a+");
	fclose(f1);
	char s[100];
	char str[100];
	memset(s,0,100);
	strcpy(s,trace_m);
	lt = time(NULL);
	ptr = localtime(&lt);
	strcat(s,"ACS-");
	strcat(s,RDATA_D);
	strftime(str, 100, rtime, ptr);
	strcat(s,str);
	strcat(s,RDATA_H);
	strcat(s,RDATA_m);
	strcat(s,".trace");

	/*
	 * opens the trace file to print message
	 */

	f1=fopen(s,"a+");
	/*
	 * trace On or Off ?
	 */
	if(trace_OnOff==1)
	{
	    struct timeval tv;
        gettimeofday(&tv,NULL);
        long milsec;
        milsec=tv.tv_usec/1000;
    	char str2[80];
        strftime(str2, 100, "%H:%M:%S", ptr);
        fprintf(f1,"%s%s:%ld %s\n",RDATA_M,str2,milsec,message);
	}
	fclose(f1);
	strcpy(Email,s_mail);
	return 0;

}

/*
 * print the log file line in this form (subject, id eoorr leveled, reports, place errora)
 *
 * input parameters of the IP address of messages ERROR Level and location error
 */

int logtr(char *message,char *ID_client,int level,const char *conff)
{
	/*
	 * first is read the config file?
	 */

	if(conf_level==5)
	{
		read_conf(f_name, s_mail,&conf_level,&trace_OnOff,rtime);
		int k=strlen(f_name);
		char tr[k-1];
		memset(tr,'\0',k-1);
		strcpy(tr,f_name);
		strcpy(trace_m,tr);
		trace_m[strlen(trace_m)-1]='\0';
		f_name[strlen(f_name)-1]='\0';
		if (f_name[strlen(f_name)-1]!='/')
		{
			strcat(f_name,"/");
		}

		if (trace_m[strlen(trace_m)-1]!='/')
		{
			strcat(trace_m,"/");
		}
		strcpy(log_m,f_name);
		k=strlen(s_mail);
		char tm[k-1];
		memset(tm,'\0',k-1);
		strcpy(tm,s_mail);
		strcpy(Email,tm);
		Email[strlen(Email)-1]='\0';
	}

	logtime(f_name,rtime);

	/*
	 * opens the log file to print message
	 */

    f=fopen(f_name,"a+");
    char str[80];
    lt = time(NULL);
    ptr = localtime(&lt);
    strftime(str, 100, "%H:%M:%S", ptr);
    struct timeval tv;
    gettimeofday(&tv,NULL);
    long milsec;
    milsec=tv.tv_usec/1000;
    char error_level[10];
    switch (level)
    {
    case 0:
    {
    	strcpy(error_level,"INFO");
        break;
    }
    case 1:
    {
    	strcpy(error_level,"DEBUG");
    	break;
    }
    case 2:
    {
    	strcpy(error_level,"WARNING");
    	break;
    }
    case 3:
    {
    	strcpy(error_level,"ERROR");
    	break;
    }
    case 4:
    {
    	strcpy(error_level,"FATAL");
    	break;
    }
    default: break;
    }

    if(level>=conf_level)
    {
    	fprintf(f,"%s%s:%ld %s %s %s %s\n",RDATA_M,str,milsec,ID_client,error_level,message,conff);
    }
    fclose(f);

  return 0;
}


